<!--Header.vue-->
<template>
    <div class="header">
      <div class="right">
        <el-popover
            placement="el-button"
            :icon="Primary"
            :width="405"
            type="red"
            trigger="click"
            popper-class="popper-user-box"
        >

          <template #reference>
            <div class="author">
              <i class="icon el-icon-s-custom" />
              {{ state.userInfo && state.userInfo.nickName || '' }}
              <i class="el-icon-caret-bottom" />
            </div>
          </template>
          <div class="nickname">
            <p>登录名：{{ state.userInfo && state.userInfo.loginUserName || '' }}</p>
            <p>昵称：{{ state.userInfo && state.userInfo.nickName || '' }}</p>
            <el-tag size="small" effect="dark" class="logout" @click="logout">退出</el-tag>
          </div>
        </el-popover>
      </div>
    </div>
  </template>
  
  <script setup>
  import { onMounted, reactive, toRefs } from 'vue'
  import { useRouter } from 'vue-router'
  import axios from '@/utils/axios'
  import { localRemove, pathMap,localGet } from '@/utils'
  const router = useRouter()
  const state = reactive({
    name: 'dashboard',
    userInfo: null, // 用户信息变量
  })
  // 初始化执行方法
  onMounted(() => {
    const pathname = window.location.hash.split('/')[1] || ''
    console.log('pathname=='+pathname)
    if (localGet('NewbeeToken')) {
      getUserInfo()
    }
  })
  // 获取用户信息
  const getUserInfo = async () => {
    const userInfo = await axios.get('/manage-api/v1/adminUser/profile')
    state.userInfo = userInfo
  }
  // 退出登录
  const logout = () => {
    axios.delete('/manage-api/v1/logout').then(() => {
      // 退出之后，将本地保存的 token  清理掉
      localRemove('token')
      // 回到登录页
      router.push({ path: '/login' })
    })
  }

  router.afterEach((to) => {
    console.log('to', to)
    const { id } = to.query
    state.name = pathMap[to.name]
  })
</script>

  
<style scoped>
.header {
  height: 50px;
  border-bottom: 1px solid #e9e9e9;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}
.right > div > .icon{
  font-size: 18px;
  margin-right: 6px;
}
.author {
  margin-left: 10px;
  cursor: pointer;
}
</style>
<style>
  .popper-user-box {
    background: url('../assets/account-banner-bg.png') 50% 50% no-repeat!important;
    background-size: cover!important;
    border-radius: 0!important;
  }
  .popper-user-box .nickname {
    position: relative;
    color: #ffffff;
  }
  .popper-user-box .nickname .logout {
    position: absolute;
    right: 0;
    top: 0;
    cursor: pointer;
  }
</style>